package com.jinlejia.erp.ui.new_house.house;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.freedom.yefeng.yfrecyclerview.FullyLinearLayoutManager;
import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.FragmentHouseTypeListBinding;
import com.jinlejia.erp.ui.common.BundleArrayListBean;
import com.jinlejia.erp.ui.common.BundleLinkedHashMap;
import com.jinlejia.erp.ui.common.IntentLinkedHashMapWrapper;
import com.jinlejia.erp.ui.new_house.adapter.HouseTypeListAdapter;

import java.util.ArrayList;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HouseTypeListFragment#newInstance} factory method to
 * create an instance of this fragment.
 * 户型列表Fragment
 */
public class HouseTypeListFragment extends Fragment {
    private View view;
    private ArrayList<Map<String, Object>> dataList;
    private BundleLinkedHashMap allData;
    public HouseTypeListFragment() {
        // Required empty public constructor
    }

    // TODO: Rename and change types and number of parameters
    public static HouseTypeListFragment newInstance(BundleArrayListBean data, BundleLinkedHashMap allData) {
        HouseTypeListFragment fragment = new HouseTypeListFragment();
        Bundle args = new Bundle();
        args.putParcelable("data", data);
        args.putParcelable("allData", allData);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            BundleArrayListBean data = getArguments().getParcelable("data");
            allData =  getArguments().getParcelable("allData");
            dataList = data.getDataList();
        }
    }

    private FragmentHouseTypeListBinding binding;
    private HouseTypeListAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_house_type_list, container, false);
            binding = DataBindingUtil.bind(view);
            initView();
        }
        // 缓存的rootView需要判断是否已经被加过parent，
        // 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }
        return view;
    }

    private void initView() {
        binding.houseTypeRecyclerview.setHasFixedSize(true);
        binding.houseTypeRecyclerview.setLayoutManager(new FullyLinearLayoutManager(getContext()));
        adapter = new HouseTypeListAdapter(new ArrayList<>());
        binding.houseTypeRecyclerview.setAdapter(adapter);
        adapter.setData(dataList);

        adapter.OnImagesListener(new HouseTypeListAdapter.OnImageListener() {
            @Override
            public void OnImageClick(int id) {
                Intent intent = new Intent(getContext(), HouseTypeDetails.class);
                intent.putExtra("picId", id);
                intent.putExtra("map",new IntentLinkedHashMapWrapper(allData.getMap()));
                startActivity(intent);
            }
        });

    }

}